package com.arges.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arges.web.common.Constant;
import com.arges.web.common.GsonUtils;
import com.arges.web.common.SysEnum.BloodType;
import com.arges.web.common.SysEnum.ExtractTime;
import com.arges.web.domain.bean.BloodBean;
import com.arges.web.domain.pojo.Blood;
import com.arges.web.page.PageInfo;
import com.arges.web.page.WebUtils;
import com.arges.web.service.BloodManager;
import com.arges.web.service.PatManager;

/**
 * 血液管理控制层
 * 
 * @author zxj
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/blood")
public class BloodController
{

	private Logger LOGGER = LoggerFactory.getLogger(BloodController.class);

	@Autowired
	private BloodManager bloodManager;

	@Autowired
	private PatManager patManager;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getPageBlood.do")
	public String getPageBlood(HttpServletRequest request, ModelMap map)
	{

		PageInfo<BloodBean> pageInfo = null;
		String mrn = request.getParameter("search_mrn");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		if (!StringUtils.isNotBlank(mrn))
		{
			mrn = "";
		}
		if (!StringUtils.isNotBlank(startDate))
		{
			startDate = "";
		}
		if (!StringUtils.isNotBlank(endDate))
		{
			endDate = "";
		}

		String pageString = request.getParameter("PageInfo");
		if (StringUtils.isNotBlank(pageString))
		{
			pageInfo = GsonUtils.stringToObject(pageString, PageInfo.class);
		}
		else
		{
			pageInfo = new PageInfo<BloodBean>();
		}

		try
		{
			PageInfo<Blood> pojopInfo = WebUtils.beanToPojoPage(pageInfo, Blood.class);
			pageInfo = bloodManager.getPageBlood(pojopInfo, mrn, startDate, endDate);
		}
		catch (Exception e)
		{
			System.out.println("getPageBlood is error. e=" + e);
		}
		map.put("chaxun", mrn);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("bloodBeanList", pageInfo.getResult());
		pageInfo.setResult(null);
		map.put("bloodPageInfo", GsonUtils.objectToJson(pageInfo));
		return "pat/blood";
	}

	/**
	 * 获取血液种类列表
	 * 
	 * @param request
	 * @return list
	 */
	@RequestMapping(value = "/getBloodTypeList.do")
	@ResponseBody
	public BloodType[] getBloodTypeList()
	{
		return BloodType.values();
	}

	/**
	 * 获取血液提取时间类型列表
	 * 
	 * @param request
	 * @return list
	 */
	@RequestMapping(value = "/getExtractTimeList.do")
	@ResponseBody
	public ExtractTime[] getExtractTimeList()
	{
		return ExtractTime.values();
	}

	@RequestMapping(value = "/saveOrUpdate.do")
	@ResponseBody
	public String saveOrUpdate(HttpServletRequest request, BloodBean bloodBean)
	{
		String result = Constant.SUCCESS;

		if (StringUtils.isBlank(bloodBean.getCyf_0()))
		{
			bloodBean.setCyf_0(null);
		}
		if (StringUtils.isBlank(bloodBean.getCyf_6()))
		{
			bloodBean.setCyf_6(null);
		}
		if (StringUtils.isBlank(bloodBean.getCyf_10()))
		{
			bloodBean.setCyf_10(null);
		}

		if (StringUtils.isBlank(bloodBean.getCea_0()))
		{
			bloodBean.setCea_0(null);
		}
		if (StringUtils.isBlank(bloodBean.getCea_6()))
		{
			bloodBean.setCea_6(null);
		}
		if (StringUtils.isBlank(bloodBean.getCea_10()))
		{
			bloodBean.setCea_10(null);
		}

		if (StringUtils.isBlank(bloodBean.getActive_0()))
		{
			bloodBean.setActive_0(null);
		}
		if (StringUtils.isBlank(bloodBean.getActive_6()))
		{
			bloodBean.setActive_6(null);
		}
		if (StringUtils.isBlank(bloodBean.getActive_10()))
		{
			bloodBean.setActive_10(null);
		}

		ExtractTime extractTime = ExtractTime.valueOf(bloodBean.getType());
		bloodBean.setType(String.valueOf(extractTime.getId()));
		BloodType bloodType = BloodType.valueOf(bloodBean.getG_type());
		bloodBean.setG_type(String.valueOf(bloodType.getId()));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try
		{
			Date date = sdf.parse(bloodBean.getG_date_String());
			bloodBean.setG_date(date);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		if (null != bloodBean.getId())
		{
			Blood blood = new Blood();
			BeanUtils.copyProperties(bloodBean, blood);
			bloodManager.update(blood);
		}
		else
		{
			Blood blood = new Blood();
			BeanUtils.copyProperties(bloodBean, blood);
			bloodManager.save(blood);
		}

		return result;
	}

	/**
	 * 根据id获取Blood
	 *
	 * @param mrn
	 * @return
	 */
	@RequestMapping(value = "/getBloodById.do")
	@ResponseBody
	public BloodBean getBloodById(HttpServletRequest request)
	{
		String id = request.getParameter("id");
		BloodBean bloodBean = new BloodBean();
		// try
		// {
		Blood blood = bloodManager.get(Long.valueOf(id));
		BeanUtils.copyProperties(blood, bloodBean);
		BloodType[] bloodTypeList = BloodType.values();
		ExtractTime[] extractTimeList = ExtractTime.values();
		for (ExtractTime extractTime : extractTimeList)
		{
			if (bloodBean.getType().equals(String.valueOf(extractTime.getId())))
			{
				bloodBean.setType(extractTime.name());
			}
		}
		for (BloodType bloodType : bloodTypeList)
		{
			if (bloodBean.getG_type().equals(String.valueOf(bloodType.getId())))
			{
				bloodBean.setG_type(bloodType.name());
			}
		}

		// // }
		// // catch (Exception e)
		// {
		// System.out.println("getBloodByMrn is error,error is" + e);
		// // bloodBean = null;
		// //// }
		return bloodBean;
	}

	/**
	 * 删除、批量删除用户
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteByIds.do")
	@ResponseBody
	public String deleteByIds(HttpServletRequest request)
	{
		String result = Constant.SUCCESS;
		String ids = request.getParameter("ids");
		try
		{
			bloodManager.deleteByIds(ids);
		}
		catch (Exception e)
		{
			System.out.println("delete Blood ByIds is error,error is" + e);
			result = Constant.ERROR;
		}
		return result;
	}
}
